package com.xiaotu.spider.maoyan.pageprocessor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.NeiDiBoxOfficeModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * NeiDiBoxOfficePageProcessor
 * @author wangyanlong
 * @date 2020年1月2日 上午9:40:04
 * 58921网站内地电影信息
 */
@Component("NeiDiBoxOfficePageProcessor")
@Scope("prototype")
public class NeiDiBoxOfficePageProcessor extends AUrlPageProcessor
{
	@Override
	protected String[] getStartRequestUrls()
	{
		return new String[] {
				"http://58921.com/alltime/2020",
				"http://58921.com/alltime/2019",
                "http://58921.com/alltime/2018",
                "http://58921.com/alltime/2017",
				"http://58921.com/alltime/2016",
                "http://58921.com/alltime/2015",
				"http://58921.com/alltime/2014",
				"http://58921.com/alltime/2013",
				"http://58921.com/alltime/2012",
				"http://58921.com/alltime/2011",
				"http://58921.com/alltime/2010"};
	}

	@Override
	protected String getSpiderType()
	{
		return Constants.SpiderTaskType.NEIDI_BOXOFFICE;
	}

	@Override
	public String getPageDataType(Page page)
	{
		String url = page.getUrl().toString();
		if (url.contains("alltime") && !url.contains("page"))
			return Constants.PageDataType.SEARCH_LIST;
		if (url.contains("alltime") && url.contains("page"))
			return Constants.PageDataType.TV_RECORD;
		if (url.contains("film"))
			return Constants.PageDataType.BASICINFO;
		else
			return Constants.PageDataType.SEARCH_LIST;
	}

	@Override
	public void process(Page page)
	{
		Map<String, Object> tvMap = page.getRequest().getExtras();
		switch (this.getPageDataType(page))
		{
			case Constants.PageDataType.SEARCH_LIST:
				this.searchListProcess(page, tvMap);
				break;
			case Constants.PageDataType.TV_RECORD:
				this.tvRecordProcess(page, tvMap);
				break;
			case Constants.PageDataType.BASICINFO:
				this.tvBasicInfoProcess(page, tvMap);
				break;
			default:
				break;
		}
	}
	
	protected void searchListProcess(Page page, Map<String, Object> tvMap){
		String pageCur = page.getHtml()
				.xpath("//li[@class='pager_count']/span/span/text()").get();
		if(StringUtil.isNotEmpty(pageCur)){
			int pageCount = Integer.parseInt(pageCur.substring(pageCur.indexOf("/")+1, pageCur.length()));
			for (int i = 0; i <= pageCount; i++) {
				PageUtils.addTargetRequest(page,page.getUrl().toString()+"?page="+i,tvMap,true);
			}
		}
		/*if(page.getUrl().toString().contains("2020")){
			PageUtils.addTargetRequest(page,page.getUrl().toString()+"?page=0",tvMap,true);
		}*/
	}
	
	protected void tvRecordProcess(Page page, Map<String, Object> tvMap){
		List<String> divList = page.getHtml()
				.xpath("//div[@class='table-responsive']/table/tbody/tr/td[3]").all();
		for (String divHtml : divList) {
			Html pageHtml = new Html(divHtml);
			String linkUrl = pageHtml.xpath("//a/@href").get();
			String tvName = pageHtml.xpath("//a/text()").get();
			tvMap.put("tvName", tvName.trim());
			PageUtils.addTargetRequest(page,linkUrl,tvMap,true);
		}
	}
	
	protected void tvBasicInfoProcess(Page page, Map<String, Object> tvMap){
		List<String> divlList = page.getHtml().xpath("//div[@class='media-body']/ul/li").all();
		List<String> actorList = new ArrayList<String>();
		List<String> directorList = new ArrayList<String>();
		String firstTime="";
		for (String divHtml : divlList) {
			Html pelHtml = new Html(divHtml);
			if(divHtml.contains("主演")){
				List<String> actorDivList = pelHtml.xpath("//li/a").all();
				for (String actorDiv : actorDivList) {
					Html actorHtml = new Html(actorDiv);
					String actor = actorHtml.xpath("//a/text()").get();
					actorList.add(actor.trim());
				}
			}
			
			if(divHtml.contains("导演")){
				String dirDivArr = pelHtml.xpath("//li/a/text()").get();
				String[] dirDivList = dirDivArr.split(" ");
				for (String dirDiv : dirDivList) {
					directorList.add(dirDiv.trim());
				}
			}
			if(divHtml.contains("上映时间")){
				firstTime = pelHtml.xpath("//li/text()").get();
				if(StringUtil.isNotEmpty(firstTime)){
					firstTime = firstTime.replace("\"", "");
				}
			}
			
		}
		page.putField(NeiDiBoxOfficeModel.class.getName(), new NeiDiBoxOfficeModel(
				tvMap.get("tvName")+"", firstTime, actorList.isEmpty()?"":String.join(",", actorList), 
						directorList.isEmpty()?"":String.join(",", directorList)));
	}
}
